<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'ajax01.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<script type="text/javascript">
		var xhr = null;
		function test(){
			//1.创建XMLHttpRequest对象
			if(window.XMLHttpRequest){
				xhr = new XMLHttpRequest();
				console.log(xhr);
			}
			//针对于IE6以下
			if(window.ActiveXObject){
				new ActiveXObject("Microsoft.XMLHTTP");
			}
			//2.设置回调函数
			xhr.onreadystatechange = function(){
				var msg = document.getElementById("msg");
				msg.innerHTML = "<span style='color:red'>"+xhr.responseText+"</span>";
			};
			//3.初始化XMLHttpRequest组件
			/*
			参数1:请求类型(get/post)
			参数2:请求地址
			参数3:是否发送异步请求(true/false)
			*/
			xhr.open("get","/ajax01/ajax.do",true);
			//4.发送请求
			xhr.send();
			}
	</script>
  </head>
  <body>
  	<button onclick="test()">测试Ajax</button>
    <div id="msg"></div>
  </body>
</html>
